package com.gaoy.springdemo.controller;

import com.gaoy.springdemo.model.MessageInfo;
import com.gaoy.springdemo.server.MessageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageServer messageServer;
    private List<MessageInfo> messageInfoList = new ArrayList<>();

//    @RequestMapping(value = "/publish",produces = "application/json")
    @RequestMapping(value = "/publish")
    public String publish(@RequestBody MessageInfo messageInfo){
        if (!StringUtils.hasLength(messageInfo.getMessage())
                ||!StringUtils.hasLength(messageInfo.getFrom())
                ||!StringUtils.hasLength(messageInfo.getTo())){
            return "{\"ok\":0}";
        }
        //messageInfoList.add(messageInfo);
        messageServer.addMessage(messageInfo);
        return "{\"ok\":1}";
    }
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageServer.queryAll();
    }

}
